﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.Utils;


namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 二级库存台账入库
    /// </summary>
    /// Title:ZMCMG1
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张坤
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张坤   [创建]
    ///
    public partial class ZMCMG1 : EFViewCmes
    {
        private DepotBlock depotBlock = new DepotBlock();//构造前台下拉列表
        public ZMCMG1()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(depotBlock.GetDepotStatus());
            base.Initialize(initData);
        }
        /// 修改前设置属性
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.sub_grid.GetCheckedEIRows();
            _view.ViewType = "tabitem";
            _view.Args.Add("fdDepotCode", this.fdDepotCode.Value.ToString());//外键赋值
            _view.Args.Add("fdDepotName", this.fdDepotName.Value.ToString());//外键赋值

            this.ViewProperty = _view;
        }

        /// <summary>
        /// 二级库存台账入库操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStoreinButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            EIInfo eiInfo = new EIInfo();
            this.OpenPopup(eiInfo, "二级库存台账入库新增", "insert", "storeIn");
            //base.TempButtonOpen(true);//按钮生效
        }

        /// <summary>
        /// 二级库存台账出库操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStoreoutButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            EIInfo eiInfo = new EIInfo();
            this.OpenPopup(eiInfo, "二级库存台账出库新增", "insert", "storeOut");
            //base.TempButtonOpen(true);//按钮生效
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.sub_grid.Refresh();
            }
            base.TempButtonOpen(true);//按钮生效
        }


        /// <summary>
        /// 包括主表、从表
        /// 新增、修改、查看之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
            if ("storeIn".Equals(_args))//构造入库新增界面
            {
                ChildWindows.LevelBaseAccountStroeIn typemanagement = new ChildWindows.LevelBaseAccountStroeIn(_dialogType);//构造一个新的修改页面
                typemanagement.fdDepotCode.Value = this.fdDepotCode.Value;
                typemanagement.fdDepotName.Value = this.fdDepotName.Value;
                typemanagement.fdType.Value      = "入库";
                typemanagement.EIDataContext = new Platform.Core.UI.EIDataContext();
                typemanagement.EIDataContext.Update(_eIInfo);
                CmesWindows _popup = new CmesWindows();         //构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;        //设置不能修改窗口大小
                _popup.Header = _title == null ? "" : _title;   //设置Title
                _popup.Content = typemanagement;                //让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
                _popup.ShowDialog();
            }

            if ("storeOut".Equals(_args))    //构造出库新增界面
            {
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.department"));
                ChildWindows.LevelBaseAccountStroeOut typemanagement = new ChildWindows.LevelBaseAccountStroeOut(_dialogType);//构造一个新的修改页面

                typemanagement.fdDepotCode.Value = this.fdDepotCode.Value;
                typemanagement.fdDepotName.Value = this.fdDepotName.Value;
                typemanagement.fdType.Value = "出库";

                typemanagement.fdMaterialCode.eViewInitParams = typemanagement.fdMaterialCode.eViewInitParams + this.fdDepotCode.Value;

                typemanagement.EIDataContext = new Platform.Core.UI.EIDataContext();
                typemanagement.EIDataContext.Update(_eIInfo);
                CmesWindows _popup = new CmesWindows();         //构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;        //设置不能修改窗口大小
                _popup.Header = _title == null ? "" : _title;   //设置Title
                _popup.Content = typemanagement;                //让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
                _popup.ShowDialog();
            }

        }
        /// <summary>
        /// 作废操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnInvalidButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            int count = this.sub_grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择需作废的库存台账信息！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("只能同时作废一条库存台账信息！", true);
                return;
            }
            else
            {
                IList<EIRow> _list = this.sub_grid.GetCheckedEIRows();
                EIRow row = _list[0];
                if (row["fdRecordStatus"].Equals("作废"))
                {
                    this.OpenEFWindowAlert("选中的信息已经是作废状态,不允许重复作废！", true);
                    return;
                }

                DialogParameters param = this.ModelDialogParameters("Confirm", "确定作废该条记录？");
                param.Closed = OnInvalidConfirmClosed;
                EFWindow.Confirm(param);
            }
        }

        /// <summary>
        /// 审批确认，点击作废按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnInvalidConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                base.Submit(OperationMode.Update, "ZMCMG1", "Invalid", DataRange.CheckedItems, DoCommitCompleted, null);
            }
        }
        /// <summary>
        /// 执行完审批操作的回调函数
        /// </summary>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
           // this.sub_grid.Refresh();
            //base.TempButtonOpen(true);//按钮生效

            if (e.EIInfo.Status == -1)
            {
                String msg = "操作失败！";
                if (e.EIInfo.GetMsg() != null || e.EIInfo.GetMsg().Equals(""))
                {
                    msg = e.EIInfo.Msg;
                }
                base.OpenEFWindowAlert(msg, true);
            }
            else
            {
                this.sub_grid.Refresh();
            }

            this.TempButtonOpen(true);//按钮生效
        }

    }
}
